﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TMDT_Nhom1A
{
    public class MyOrderProduct_Online
    {
        private string _electricDeviceID;

        public string ElectricDeviceID
        {
            get { return _electricDeviceID; }
            set { _electricDeviceID = value; }
        }
        private string _ElectricDeviceName;

        public string ElectricDeviceName
        {
            get { return _ElectricDeviceName; }
            set { _ElectricDeviceName = value; }
        }
        private int _quantity;

        public int Quantity
        {
            get { return _quantity; }
            set { _quantity = value; }
        }
        private decimal _price;

        public decimal Price
        {
            get { return _price; }
            set { _price = value; }
        }
        private decimal _priceCoupon;

        public decimal PriceCoupon
        {
            get { return _priceCoupon; }
            set { _priceCoupon = value; }
        }
        private decimal _totalPrice;

        public decimal TotalPrice
        {
            get { return _totalPrice; }
            set { _totalPrice = value; }
        }

        private decimal _retailPrice;

        public decimal RetailPrice
        {
            get { return _retailPrice; }
            set { _retailPrice = value; }
        }

        private decimal _dealerPrice;

        public decimal DealerPrice
        {
            get { return _dealerPrice; }
            set { _dealerPrice = value; }
        }
    }

    public class Cart
    {
        private string _orderB2BID;

        public string OrderB2BID
        {
            get { return _orderB2BID; }
            set { _orderB2BID = value; }
        }

        private List<MyOrderProduct_Online> listorderproduct;

        public List<MyOrderProduct_Online> GetList()
        {
            return listorderproduct;
        }

        public void AddToCart(List<MyOrderProduct_Online> cart)
        {
            listorderproduct = cart;
        }


        public decimal CalulateTotalPrice()
        {
            decimal TotalPrice = 0;
            for (int i = 0; i < listorderproduct.Count; i++)
            {
                TotalPrice += listorderproduct[i].TotalPrice;
            }
            return TotalPrice;
        }

        public void CalculatePrice(MyOrderProduct_Online OrderProduct)
        {
            if (OrderProduct.PriceCoupon == 0)
                OrderProduct.TotalPrice = OrderProduct.Quantity * OrderProduct.Price;
            else
                OrderProduct.TotalPrice = OrderProduct.PriceCoupon * OrderProduct.Quantity;
        }

        public void UpdateCart(int index, int quantity)
        {
            listorderproduct[index].Quantity = quantity;
            if (listorderproduct[index].Quantity > 10)
            {
                listorderproduct[index].Price = listorderproduct[index].DealerPrice;
            }
            else
            {
                listorderproduct[index].Price = listorderproduct[index].RetailPrice;
            }
            CalculatePrice(listorderproduct[index]);
        }



        public void AddToCart(MyOrderProduct_Online orderproduct)
        {
            int temp = 0;
            for (int i = 0; i < listorderproduct.Count; i++)
            {
                if (listorderproduct[i].ElectricDeviceID == orderproduct.ElectricDeviceID)
                {
                    temp = 1;
                    listorderproduct[i].Quantity += orderproduct.Quantity;
                    if (listorderproduct[i].PriceCoupon != 0)
                    {
                        listorderproduct[i].Price = listorderproduct[i].PriceCoupon;
                    }
                    else
                    {
                        if (listorderproduct[i].Quantity > 10)
                        {
                            listorderproduct[i].Price = listorderproduct[i].DealerPrice;
                        }
                        else
                        {
                            listorderproduct[i].Price = listorderproduct[i].RetailPrice;
                        }
                    }
                    CalculatePrice(listorderproduct[i]);
                }
            }
            if (temp == 0)
            {
                if (orderproduct.PriceCoupon != 0)
                {
                    orderproduct.Price = orderproduct.PriceCoupon;
                }
                else
                {
                    if (orderproduct.Quantity > 10)
                    {
                        orderproduct.Price = orderproduct.DealerPrice;
                    }
                    else
                    {
                        orderproduct.Price = orderproduct.RetailPrice;
                    }
                }

                listorderproduct.Add(orderproduct);
                CalculatePrice(orderproduct);
            }
            CalulateTotalPrice();
        }

        public void RemoveOrderProductOutCart(int index)
        {
            listorderproduct.RemoveAt(index);

        }

        public Cart()
        {
            listorderproduct = new List<MyOrderProduct_Online>();
        }

        public Cart(List<MyOrderProduct_Online> listorderproduct)
        {
            listorderproduct = new List<MyOrderProduct_Online>();
            for (int i = 0; i < listorderproduct.Count; i++)
            {
                MyOrderProduct_Online detailorderproduct = new MyOrderProduct_Online();
                detailorderproduct.ElectricDeviceID = listorderproduct[i].ElectricDeviceID;
                detailorderproduct.ElectricDeviceName = listorderproduct[i].ElectricDeviceName;
                detailorderproduct.DealerPrice = listorderproduct[i].DealerPrice;
                detailorderproduct.RetailPrice = listorderproduct[i].RetailPrice;
                detailorderproduct.PriceCoupon = listorderproduct[i].PriceCoupon;
                detailorderproduct.Quantity = listorderproduct[i].Quantity;
                detailorderproduct.TotalPrice = listorderproduct[i].TotalPrice;
                listorderproduct.Add(detailorderproduct);
            }
        }


    }
}
